package com.yogee.topseller.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;

import com.yogee.core.base.HttpFragment;
import com.yogee.topseller.R;
import com.yogee.topseller.bean.EventBean;
import com.yogee.topseller.fragment.collect.CollectBodyFragment;
import com.yogee.topseller.fragment.collect.CollectBodyOtherFragment;
import com.yogee.topseller.utils.TabLayoutUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/20.
 */

public class CollectFragment extends HttpFragment{
    @BindView(R.id.collect_tab)
    TabLayout collectTab;
    @BindView(R.id.collect_vp)
    ViewPager collectVp;
    @BindView(R.id.collect_layout)
    LinearLayout collectLayout;
    private List<String> titles;
    private List<Fragment> fragments;
    private String tabNum;
    private CollectBodyFragment bodyFragment;
    private CollectBodyOtherFragment bodyOtherFragment;
    private static String types;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_collect;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        collectVp.setOffscreenPageLimit(1);
        titles = new ArrayList<>();
        fragments = new ArrayList<>();
        titles.add("收藏");
        titles.add("分享");
        titles.add("浏览");
        titles.add("搜索");
        titles.add("对比");
        titles.add("PK");
        
        for (int i = 0; i < 3; i++) {
            Bundle bundle = new Bundle();
            bundle.putInt("collectType",i);
            bodyFragment = new CollectBodyFragment();
            bodyFragment.setArguments(bundle);
            fragments.add(bodyFragment);
        }
        for (int i = 0; i < 3; i++) {
            Bundle bundle = new Bundle();
            bundle.putInt("collectType",i);
            bodyOtherFragment = new CollectBodyOtherFragment();
            bodyOtherFragment.setArguments(bundle);
            fragments.add(bodyOtherFragment);
        }
        new TabLayoutUtil(getActivity().getSupportFragmentManager(), collectLayout, fragments, titles, 100);
    }

    @Subscribe
    public void eventBean(EventBean eventBean){
//        new TabLayoutUtil(getActivity().getSupportFragmentManager(), collectLayout, fragments, titles, 100,eventBean.getType());

        String type = eventBean.getType();
        try{
            int getType = Integer.valueOf(type);
            collectVp.setCurrentItem(getType);
        }catch (Exception e){

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
